package com.itsu.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itsu.dao.DeptDao;
import com.itsu.dao.UserDao;
import com.itsu.domain.Dept;
import com.itsu.domain.Module;
import com.itsu.domain.User;
import com.itsu.service.UserService;
import com.itsu.utils.MapperUtils;

import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao mapper;

    @Override
    public void save(User user) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            mapper.save(user);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void delete(User user) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            mapper.delete(user);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void update(User user) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            mapper.update(user);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public User findById(String id) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            User user = mapper.findById(id);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<User> findAll() {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            List<User> all = mapper.findAll();
            return all;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public PageInfo<User> findAll(int pageNum, int pageSize) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<User> all = mapper.findAll();
            PageInfo<User> userPageInfo = new PageInfo<>(all);
            return userPageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public User findByEmail(String email) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            User user = mapper.findByEmail(email);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public void deleteRoleUserByUserId(String userId) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            mapper.deleteRoleUserByUserId(userId);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void addRoleUser(String userId, String roleId) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            mapper.addRoleUser(userId, roleId);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public List<Module> findModulesById(String userId) {
        try {
            mapper = MapperUtils.getMapper(UserDao.class);
            List<Module> list = mapper.findModulesById(userId);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

}
